﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using alib.Collections.ReadOnly;
using alib.Enumerable;


namespace agree.itsdb
{
	public class Schema : ArrayWrapper<FieldInfo>
	{
		public Schema(IEnumerable<FieldInfo> iefi, int c)
			: base(iefi, c)
		{
		}
	}

	public class Schema<T> : Schema where T : entity
	{
		public Schema(ICollection<FieldInfo> iefi)
			: base(iefi, iefi.Count)
		{
		}
		public Schema(IList<String> ies)
			: base(ies.Select(s =>
			{
				FieldInfo fi = typeof(T).GetField("_" + s.Replace('-', '_'), BindingFlags.Instance | BindingFlags.NonPublic);
				if (fi == null)
				{
					String msg = String.Format("The field '{0}' in the schema for '{1}' is not recognized.", s, typeof(T).Name);
					throw new Exception(msg);
				}
				return fi;
			}), ies.Count)
		{
		}
	};
}